package com.lw.extraceim.loader;

import android.content.Context;

import androidx.annotation.NonNull;

import com.lw.extraceim.app.AppConstants;
import com.lw.extraceim.loader.base.BaseLoader;
import com.lw.extraceim.model.ExpressSheet;
import com.lw.extraceim.util.JsonUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 快件信息的加载类
 * @Date : 2019/5/7 19:13
 */
public class ExpressSheetLoader extends BaseLoader {

    public ExpressSheetLoader(Context context) {
        super(context);
    }

    /**
     * 保存快件信息
     *
     * @param es
     */
    public void save(@NonNull ExpressSheet es, @NonNull Integer userId, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/create/" + userId;
        String esJson = JsonUtils.toJson(es);
        execute(url, esJson, callback);
    }

    /**
     * 通过快件编号获取快件信息
     *
     * @param esId
     * @param callback
     */
    public void get(@NonNull String esId, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/get/" + esId;
        execute(url, AppConstants.GET, null, callback);
    }

    /**
     * 通过快件信息中的属性值查找快件信息
     *
     * @param es
     * @param callback
     */
    public void getEsList(@NonNull ExpressSheet es, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/list";
        String esJson = JsonUtils.toJson(es);
        execute(url, esJson, callback);
    }

    /**
     * 通过用户ID和快件状态查找快件列表信息
     *
     * @param userId
     * @param status
     */
    public void getEsList(int userId, int status, @NonNull Callback callback) {
//        String url = AppConstants.ES_SERVICE + "/list";
//        Map<String,String> form = new HashMap<>();
//        form.put("userId",userId + "");
//        form.put("status",status + "");
//        execute(url,AppConstants.GET,form,callback);
        ExpressSheet expressSheet = new ExpressSheet();
        expressSheet.setAccepter(userId + "");
        expressSheet.setStatus(status);
        getEsList(expressSheet, callback);
    }

    /**
     * 修改快件信息
     *
     * @param expressSheet
     * @param callback
     */
    public void update(ExpressSheet expressSheet, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/update";
        String esJson = JsonUtils.toJson(expressSheet);
        execute(url, esJson, callback);
    }
//    public void getEsList(@NonNull Integer userId,@NonNull Callback callback){
//
//    }

    /**
     * 获取包裹中的快件信息
     *
     * @param packageId
     * @param callback
     */
    public void getEsList(String packageId, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/getEsFormPakcage/" + packageId;
        execute(url, AppConstants.GET, null, callback);
    }

    /**
     * 派送包裹
     *
     * @param esId
     * @param userId
     * @param callback
     */
    public void dispatchEs(String esId, int userId, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/dispatchExpressSheet/" + esId + "/" + userId;
        execute(url, AppConstants.GET, null, callback);
    }

    /**
     * 加载正在派送的快件
     *
     * @param userId
     * @param callback
     */
    public void loadDispatchEs(int userId, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/listByDispatch";
        Map<String, String> form = new HashMap<>();
        form.put("userId", userId + "");
        form.put("status", ExpressSheet.STATUS.STATUS_DISPATCHED + "");
        execute(url, AppConstants.POST, form, callback);
    }

    /**
     * 交付快件
     *
     * @param esId
     * @param userId
     * @param callback
     */
    public void deliverEs(String esId, int userId, @NonNull Callback callback) {
        String url = AppConstants.ES_SERVICE + "/deliver/" + esId + "/" + userId;
        execute(url, AppConstants.GET, null, callback);
    }
}
